package io.renren.modules.ranking.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.util.TextUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.ranking.entity.IllegalVehicleEntity;
import io.renren.modules.ranking.service.IllegalVehicleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;


/**
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-24 22:07:10
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("ranking/illegalvehicle")
public class IllegalVehicleController {
    @Autowired
    private IllegalVehicleService illegalVehicleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ranking:illegalvehicle:list")
    public R list(@RequestParam Map<String, Object> params) {

        //如果选择违法类型查询，解码
        if (params.containsKey("illegalType")) {
            params.put("illegalType", URLDecoder.decode(params.get("illegalType").toString()));
            System.out.print(params.get("illegalType"));
        }
        //查询列表数据
        Query query = new Query(params);

        List<IllegalVehicleEntity> illegalVehicleList = illegalVehicleService.queryList(query);
        int total = illegalVehicleService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(illegalVehicleList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ranking:illegalvehicle:info")
    public R info(@PathVariable("id") Integer id) {
        IllegalVehicleEntity illegalVehicle = illegalVehicleService.queryObject(id);

        return R.ok().put("illegalVehicle", illegalVehicle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ranking:illegalvehicle:save")
    public R save(@RequestBody IllegalVehicleEntity illegalVehicle) {
        if (isCarnumberNO(illegalVehicle.getVehiclePlate())) {
           /* //查询列表数据
            List<IllegalVehicleEntity> illegalVehicleList = illegalVehicleService.queryList(new HashMap<>());
            for (IllegalVehicleEntity illegalVehicleEntity : illegalVehicleList) {
                if (illegalVehicle.getRanking().equals(illegalVehicleEntity.getRanking())) {
                    return R.error().put("msg", "序号已存在，请重新输入！");
                }
            }*/
            illegalVehicle.setIllegalType(URLDecoder.decode(illegalVehicle.getIllegalType()));
            illegalVehicleService.save(illegalVehicle);
            return R.ok();
        } else {
            return R.error().put("msg", "车牌号格式格式有误，请重新输入");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ranking:illegalvehicle:update")
    public R update(@RequestBody IllegalVehicleEntity illegalVehicle) {
        if (isCarnumberNO(illegalVehicle.getVehiclePlate())) {
           /* IllegalVehicleEntity illegalVehicleEntity = illegalVehicleService.queryRanking(illegalVehicle.getRanking());
            if (illegalVehicleEntity.getRepeatCount() == 0 || illegalVehicleEntity.getRepeatCount() == 1) {
                illegalVehicle.setIllegalType(URLDecoder.decode(illegalVehicle.getIllegalType()));
                illegalVehicleService.update(illegalVehicle);
                return R.ok();
            }
            if (illegalVehicleEntity.getRepeatCount() > 1) {
                return R.error().put("msg", "序号已存在，请重新输入");
            }*/
            illegalVehicle.setIllegalType(URLDecoder.decode(illegalVehicle.getIllegalType()));
            illegalVehicleService.update(illegalVehicle);
            return R.ok();
        }
        return R.error().put("msg", "车牌号格式格式有误，请重新输入");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ranking:illegalvehicle:delete")
    public R delete(@RequestBody Integer[] ids) {
        illegalVehicleService.deleteBatch(ids);

        return R.ok();
    }

    public static boolean isCarnumberNO(String carnumber) {
        String carnumRegex = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})";
        if (TextUtils.isEmpty(carnumber)) {
            return false;
        } else {
            return carnumber.matches(carnumRegex);
        }
    }
}
